package com.lfk.demo.Ability;

import ohos.aafwk.ability.Ability;

import com.lfk.justwetools.Component.CodeView.CodeView;
import com.lfk.demo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.utils.net.Uri;

import java.io.File;


public class CodeAbility extends Ability {
    private CodeView codeView;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setPattern(ResourceTable.Pattern_base);
        setUIContent(ResourceTable.Layout_ability_code);

        findComponentById(ResourceTable.Id_action_code).setClickedListener(this::onActionCode);

        codeView = (CodeView)findComponentById(ResourceTable.Id_mcodeview);

        File dir = null;
        Uri fileUri = getIntent().getUri();
        if (fileUri != null) {
            dir = new File(fileUri.getDecodedPath());
        }

        if (dir != null) {
            codeView.setDirSource(dir);
            ((Text)findComponentById(ResourceTable.Id_sub_title)).setText(dir.getName());
        }
        else
            terminateAbility();

    }

    public void onActionCode(Component component) {
        if (!codeView.isEditable()) {
            ((Button)component).setText("完成");
            codeView.setContentEditable(true);
        } else {
            ((Button)component).setText("编辑");
            codeView.setContentEditable(false);
        }
    }
}
